package com.lovo.emp.frame;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.lovo.common.frame.MainFrame;
import com.lovo.common.model.DeptBean;
import com.lovo.common.model.EmpBean;
import com.lovo.common.model.WorkBean;
import com.lovo.common.service.ISysDictService;
import com.lovo.common.service.impl.SysDictServiceImpl;
import com.lovo.dept.service.IDeptService;
import com.lovo.dept.service.impl.DeptServiceImpl;
import com.lovo.emp.service.IEmpService;
import com.lovo.emp.service.impl.EmpServiceImpl;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoDate;
import com.lovo.netCRM.component.LovoFileChooser;
import com.lovo.netCRM.component.LovoLabel;
import com.lovo.netCRM.component.LovoRadioButton;
import com.lovo.netCRM.component.LovoTitleLabel;
import com.lovo.netCRM.component.LovoTxt;
import com.lovo.system.service.ILogService;
import com.lovo.system.service.impl.LogServiceImpl;


/**
 * 员工增加对话框
 * @author 白德柯
 *
 */
public class EmpAddDialog extends JDialog{


	/**姓名文本框*/
	private LovoTxt nameTxt = new LovoTxt("姓    名",40,90,this);
	/**性别单选钮*/
	private LovoRadioButton genderRadio = new LovoRadioButton("性    别",new String[]{"男","女"},300,90,this);
	/**登录名文本框*/
	private LovoTxt loginNameTxt = new LovoTxt("登录名",40,130,this);
	/**文化程度下拉框*/
	private LovoTxt educationTxt = new LovoTxt("文化程度",300,130,this);
	/**出生日期*/
	private LovoDate bornDate = new LovoDate("出生日期",40,170,this);
	/**联系方式文本框*/
	private LovoTxt phoneTxt = new LovoTxt("联系方式",300,170,this);
	/**所属专业文本框*/
	private LovoTxt subjectTxt = new LovoTxt("所属专业",40,210,this);
	/**政治面貌下拉框*/
	private LovoTxt politicalTxt = new LovoTxt("政治面貌",300,210,this);
	/**家庭地址文本框*/
	private LovoTxt addrTxt = new LovoTxt("家庭地址",40,250,this);
	/**工作职位下拉框*/
	private LovoComboBox<WorkBean> workComboBox;
	/**所在部门下拉框*/
	private LovoComboBox<DeptBean> deptComboBox;
	/**员工主面板*/
	private EmpPanel empPanel;
	/**头像*/
	private LovoFileChooser faceTxt = new LovoFileChooser(this);
	private IEmpService empService = new EmpServiceImpl();
	private IDeptService deptService = new DeptServiceImpl();
	private ILogService logService = new LogServiceImpl();

	public EmpAddDialog(JFrame jf,EmpPanel empPanel){
		super(jf,true);
		this.empPanel = empPanel;
		this.setLayout(null);
		this.setTitle("员工增加");
		this.init();
		
		Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) screensize.getWidth() / 2 - 390;
		int y = (int) screensize.getHeight() / 2 - 240;
		this.setBounds(x, y, 780, 480);
		this.setVisible(true);
	}
	/**
	 * 初始化
	 *
	 */
	private void init(){
		LovoTitleLabel titleLabel = new LovoTitleLabel("员工增加",this);
		titleLabel.setLocation(300, titleLabel.getY());
		faceTxt.setBounds(550, 90, 150, 180); 
		this.initComboBox();
		
		LovoButton addButton = new LovoButton(300,330,"添加","image/add.png",this);
		addButton.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				boolean isOk = add();
				if(isOk){
					System.out.println(EmpAddDialog.this.getOwner());
					// 记录日志
					MainFrame mainFrame = (MainFrame)(EmpAddDialog.this.getOwner());
					logService.add(mainFrame.getLoginUser(),"完成员工增加");
					// 重新渲染表格
					empPanel.initData();
					EmpAddDialog.this.dispose();
				}else{
					JOptionPane.showMessageDialog(EmpAddDialog.this,"增加失败");
				}
			}});
		
	}
	
	//--------------------------------------------
	/**
	 * 初始化下拉框
	 *
	 */
	private void initComboBox(){
		//添加工作职位List集合
		this.workComboBox = new LovoComboBox("工作职位",new ArrayList(),300,250,this);
		//添加所属部门List集合并初始数据
		this.deptComboBox = new LovoComboBox("所属部门",deptService.findAll(),40,290,this);



	}

	/**
	 * 添加操作
	 *
	 */
	private boolean add(){
		EmpBean empBean = new EmpBean();
		empBean.setName(nameTxt.getText());
		empBean.setGender(genderRadio.getItem());
		empBean.setLoginName(loginNameTxt.getText());
		empBean.setEducation(educationTxt.getText());
		if(!bornDate.getText().equals("")){
			empBean.setBorn(LocalDate.parse(bornDate.getText()));
		}
		empBean.setPhone(phoneTxt.getText());
		empBean.setSubject(subjectTxt.getText());
		empBean.setPolitical(politicalTxt.getText());
		empBean.setAddr(addrTxt.getText());
		empBean.setDeptId(deptComboBox.getItem().getId());
		empBean.setFace(faceTxt.getFilePath());
		if(empService.add(empBean) > 0){
			return true;
		}
		return false;
	}

}
